import 'package:flutter/widgets.dart';
import '../../public.dart';

typedef SelectCompTapCallback = void Function(SimpleKeyModel model);

/// 头部select筛选
class SelectComp extends StatefulWidget {
  final List<SimpleKeyModel> lst;
  final SelectCompTapCallback onTap;

  SelectComp({Key key, this.lst, this.onTap}) : super(key: key);

  @override
  SelectCompState createState() => SelectCompState();
}

class SelectCompState extends State<SelectComp> {
  var selectIndex = 0; // 选中节点

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  isSelect(int index) {
    return selectIndex == index;
  }

  @override
  Widget build(BuildContext context) {
    return SliverGrid(
        delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
          var column = widget.lst[index];
          return GestureDetector(
              onTap: () {
                selectIndex = index;
                if (widget.onTap != null) {
                  widget.onTap(column);
                }
                setState(() {});
              },
              child: Container(
                  child: Column(children: [
                Text(
                  column.label,
                  style: TextStyle(
                      color: isSelect(index)
                          ? Colors.white
                          : ColorUtil.rgba(255, 255, 255, 0.6)),
                ),
                Visibility(
                  child: Container(
                      margin: EdgeInsets.only(top: 4),
                      padding: EdgeInsets.only(top: 4),
                      child: Text(column.label),
                      height: 4,
                      decoration: BoxDecoration(
                          color: ColorUtil.rgba(255, 215, 59, 1),
                          borderRadius: BorderRadius.all(Radius.circular(12)))),
                  visible: isSelect(index),
                )
              ])));
        }, childCount: widget.lst.length),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 6,
            crossAxisSpacing: 20,
            mainAxisSpacing: 4,
            childAspectRatio: 1));
  }
}
